package com.example.casper.Experiment2024;

import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.lifecycle.Lifecycle;

import androidx.lifecycle.ViewModelProvider;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import com.example.casper.Experiment2024.gone.ListFragment;

import com.example.casper.Experiment2024.gone.SharedViewModel;
import com.example.casper.Experiment2024.gone.TencentMapsFragment;


import com.example.casper.Experiment2024.share.ShareFragment;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;


public class Hello3547Activity extends AppCompatActivity {
    private final String[] tabHeaderStrings = {"location", "Social","News", "Maps"};
    private SharedViewModel sharedViewModel;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hello3547);
        sharedViewModel = new ViewModelProvider(this).get(SharedViewModel.class);
        // 获取ViewPager2和TabLayout的实例
        ViewPager2 viewPager = findViewById(R.id.view_pager);
        TabLayout tabLayout = findViewById(R.id.tab_layout);

        // 创建适配器
        FragmentAdapter fragmentAdapter = new FragmentAdapter(getSupportFragmentManager(), getLifecycle());
        viewPager.setAdapter(fragmentAdapter);

        // 设置TabLayout与ViewPager2的绑定
        new TabLayoutMediator(tabLayout, viewPager, (tab, position) -> {
            // 设置Tab的标题
            tab.setText(tabHeaderStrings[position]);
        }).attach();
    }
        // 监听页面变化以确保ListFragment已附加
//        viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
//            @Override
//            public void onPageSelected(int position) {
//                super.onPageSelected(position);
//                if (position == 0) {
//                    listFragment = (ListFragment) getSupportFragmentManager().findFragmentByTag("f" + 0);
//                }
//            }
//        });

//tag分类
//        // 获取 ViewModel 实例
//        sharedViewModel = new ViewModelProvider(this).get(SharedViewModel.class);
//        // 初始化省份选择Spinner
//        spinnerProvince = findViewById(R.id.spinner_province);
//        initializeProvinceSpinner();
//
//        // 设置 Spinner 的选项选择监听器
//        spinnerProvince.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
//            @Override
//            public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
//
//                selectedProvince = parentView.getItemAtPosition(position).toString();
//                // 根据选中的省份做相应的处理
//
//                onProvinceSelected(selectedProvince);
//                sharedViewModel.setSelectedProvince(selectedProvince);
//            }
//
//            @Override
//            public void onNothingSelected(AdapterView<?> parentView) {
//                onProvinceSelected("全部");
//                sharedViewModel.setSelectedProvince("全部");
//            }
//        });


//添加
//        addButton = findViewById(R.id.add_button);
//
//        addButton.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                // 确保Fragment已附加并且当前标签为“全部”
//                if (listFragment != null && listFragment.isAdded() ) {
//                    listFragment.addLocation();
//                } else {
//                    // 处理Fragment未附加或当前标签不为“全部”的情况
//                    Toast.makeText(Hello3547Activity.this, "error_add", Toast.LENGTH_SHORT).show();
//                }
//            }
//        });
//截图
//        PrtScButton = findViewById(R.id.PrtSc_button);

        // 为按钮设置点击事件
//        PrtScButton.setOnClickListener(v -> captureScreen());
//    }
    // 响应函数：初始化 Spinner 省份列表
//    private void initializeProvinceSpinner() {
//        // 模拟省份数据
//        ArrayList<String> provinces = new ArrayList<>(Arrays.asList(
//                "全部","北京市", "天津市", "上海市", "重庆市",
//                "河北省", "山西省", "辽宁省", "吉林省", "黑龙江省",
//                "江苏省", "浙江省", "安徽省", "福建省", "江西省", "山东省",
//                "河南省", "湖北省", "湖南省", "广东省", "海南省",
//                "四川省", "贵州省", "云南省", "陕西省", "甘肃省", "青海省",
//                "台湾省", "内蒙古自治区", "广西壮族自治区", "西藏自治区",
//                "宁夏回族自治区", "新疆维吾尔自治区", "香港特别行政区", "澳门特别行政区"
//        ));;
//        ArrayAdapter<String> provinceAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, provinces);
//        provinceAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//        spinnerProvince.setAdapter(provinceAdapter);
//    }
//
//    public void onProvinceSelected(String province) {
//        if (listFragment != null && listFragment.isAdded()) {
//            listFragment.showLocationsByProvince(province);
//        }
//
//    }


    public class FragmentAdapter extends FragmentStateAdapter {

        public FragmentAdapter(@NonNull FragmentManager fragmentManager, @NonNull Lifecycle lifecycle) {
            super(fragmentManager, lifecycle);
        }

        @NonNull
        @Override
        public Fragment createFragment(int position) {
            // 根据位置返回对应的Fragment实例
            switch (position) {
                case 0:
                    return ListFragment.newInstance();
                case 1:
                    return ShareFragment.newInstance();
                case 2:
                    return WebViewFragment.newInstance("https://www.ctrip.com");
                case 3:
                    return new TencentMapsFragment();
                default:
                    return null;
            }
        }

        @Override
        public int getItemCount() {
            return tabHeaderStrings.length;
        }
    }

}